package com.tepth.maintenancesystem.untils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.util.Log;

public class DateUtils {
	/**
     * 通过年份和月份 得到当月的日子
     * 
     * @param year
     * @param month
     * @return
     */
    public static int getMonthDays(int year, int month) {
		month++;
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12: 
		    return 31;
		case 4:
		case 6:
		case 9:
		case 11: 
		    return 30;
		case 2:
			if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
				return 29;
			}else{
				return 28;
			}
		default:
			return  -1;
		}
    }
    /**
     * 返回当前月份1号位于周几
     * @param year
     * 		年份
     * @param month
     * 		月份，传入系统获取的，不需要正常的
     * @return
     * 	日：1		一：2		二：3		三：4		四：5		五：6		六：7
     */
    public static int getFirstDayWeek(int year, int month){
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(year, month, 1);
    	Log.d("DateView", "DateView:First:" + calendar.getFirstDayOfWeek());
    	return calendar.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取当前的日期
     * @param day
     * @return
     */
	public static String getNowCalendar(int day){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.DAY_OF_YEAR, day);
        Date date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 获取指定日期的后一天
     * @param specifiedDay
     * @param day
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay, int day) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR, day);
        return sdf.format(c.getTime());
    }

    /**
     * 字符串前面补0
     * @param str
     * @param strLength
     * @return
     */
    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        StringBuffer sb = null;
        while (strLen < strLength) {
            sb = new StringBuffer();
            sb.append("0").append(str);// 左补0
            // sb.append(str).append("0");//右补0
            str = sb.toString();
            strLen = str.length();
        }
        return str;
    }

    /**
     * 获取一年前的日期
     * @return
     */
    public static String getDateBeforeYear(){
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.add(Calendar.YEAR, -1); //月份减1
        Date lastMonth = ca.getTime(); //结果
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(lastMonth);
    }

    /**
     * 将指定的日期字符串转化成Calendar
     * @param dateString
     * @return
     * @throws ParseException
     */
    public static Calendar getDateFromString(String dateString) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sf.parse(dateString));
        return calendar;
    }

    /**
     * 获取三个月前的日期
     * @param month 几个月前
     * @return
     */
    public static String getBeforeMonthCalendar(int month){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.MONTH, month);
        Date date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 将未完成任务的日期集合转化为HashMap
     * @param dateArrayList
     * @return
     */
    public static HashMap<Integer,List<Integer>> getDateFromMonthArrayList(ArrayList<String> dateArrayList){
        HashMap<Integer,List<Integer>> notFinishWorkMap=new HashMap<>();
        try{
            int last_month=0;
            List<Integer> days=new ArrayList<>();
            for(int i=0;i<dateArrayList.size();i++){
                Calendar calendar=getDateFromString(dateArrayList.get(i));
                int temp_month=calendar.get(Calendar.MONTH)+1;
                if(temp_month!=last_month){
                    last_month=temp_month;
                    days=new ArrayList<>();
                }
                days.add(calendar.get(Calendar.DAY_OF_MONTH));
                notFinishWorkMap.put(last_month,days);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return notFinishWorkMap;
    }

    /**
     * 获取指定日期月份
     * @return
     */
    public static int getDateMonthFormString(String dateString) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();//得到一个Calendar的实例
        calendar.setTime(sf.parse(dateString));
        return calendar.get(Calendar.MONTH)+1;
    }

    /**
     * 获取当前月份
     * @return
     */
    public static int getNowMonth() {
        Calendar calendar = Calendar.getInstance();//得到一个Calendar的实例
        return calendar.get(Calendar.MONTH)+1;
    }
}
